﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace EnCor.Logging
{
    public class LogEntry
    {
        private string _Message = string.Empty;

        //private int _Priority = -1;
        //private int _EventId = 1;

        private LogLevel _Level = LogLevel.Information;
        private string _LoggerName;

        private IDictionary<string, object> _ExtendProperties;
        private Exception _Exception;
        private string _AppDomainName;
        private string _ThreadName;
        private DateTime _TimeStamp = DateTime.MaxValue;

        private bool _ThreadNameInitialized = false;
        private bool _AppDomainNameInitialized = false;
        private bool _TimeStampInitialized = false;

        public LogEntry()
        { 
        
        }

        public LogEntry(object message, LogLevel level):this(message, level, null)
        { 
        
        }

        public LogEntry(object message, LogLevel level, Exception exception)
        {
            _Message = message.ToString();
            _Level = level;
            _Exception = exception;
        }

        public string Message
        {
            get { return _Message; }
            set { _Message = value; }
        }

        public LogLevel Level
        {
            get { return _Level; }
            set { _Level = value; }
        }

        public IDictionary<string, object> ExtendProperties
        {
            get
            {
                if (_ExtendProperties == null)
                {
                    _ExtendProperties = new Dictionary<string, object>();
                }
                return _ExtendProperties;
            }
            set
            {
                _ExtendProperties = value;
            }
        }

        public Exception Exception
        {
            get { return _Exception; }
            set { _Exception = value; }
        }

        public string LoggerName
        {
            get { return _LoggerName; }
            set { _LoggerName = value; }
        }

        public string AppDomainName
        {
            get
            {
                if (!_AppDomainNameInitialized)
                {
                    InitializeAppDomainName();
                }

                return this._AppDomainName;
            }
            set
            {
                this._AppDomainName = value;
                _AppDomainNameInitialized = true;
            }
        }

        public string ThreadName
        {
            get
            {
                if (!_ThreadNameInitialized)
                {
                    InitializeThreadName();
                }

                return this._ThreadName;
            }
            set
            {
                this._ThreadName = value;
                _ThreadNameInitialized = true;
            }
        }

        public DateTime TimeStamp
        {
            get
            {
                if (!_TimeStampInitialized)
                {
                    InitializeTimeStamp();
                }

                return this._TimeStamp;
            }
            set
            {
                this._TimeStamp = value;
                _TimeStampInitialized = true;
            }
        }

        private void InitializeTimeStamp()
        {
            this.TimeStamp = DateTime.UtcNow;
        }

        private void InitializeThreadName()
        {
            this.ThreadName = Thread.CurrentThread.Name;
        }

        private void InitializeAppDomainName()
        {
            this.AppDomainName = AppDomain.CurrentDomain.FriendlyName;
        }
    }
}
